<?php
/********************************************************
 *
 * 此文件的作用:
 *		后台控制器
 *
 * @copyright Copyright (c) 2008-2009
 * @author 刘昌功 lcg635@126.com
 *
 ********************************************************/
defined('APP_DIR') or die('404 Not Found');

FLEA::loadClass('Common_Action');

class Controller_Admin extends Common_Action {
	
	function __construct($ctlName)   {
		parent::__construct($ctlName);
	}

	function index() {
		$this->_out('index');
	}

	function main() {
		echo '<center><h2>后台公告</h2></center>';
		echo '<p>市场部的同事请注意：</p>';
		echo '<p>1.为方便管理，市场部发布信息不再在<font color=red>“常用功能”</font>处发布，改为在<font color=red>“市场部管理-文章管理-文章发布”</font>，查看已发布的信息亦是在此处</P>';
		echo '<p>2.以后市场部要发布家教兼职的<font color=red>招聘信息</font>时，请在文章类型处选择<font color=red>“家教”</font></p>';
		echo '<p>3.要发布<font color=red>非招聘类的新闻</font>时，请在文章类型处选择<font color=red>“新闻”</font></p>';

	}

	function newsNew() {
		$_modNews = FLEA::getSingleton('Model_News');
		if ( $this->_isPOST() ) {
			$_modNews->insert($_POST);
			$this->_throwRemind('添加成功', '新闻已经添加', null, $this->_url('newsAll'));
		} else {
			$viewData = array(
				'form' => $_modNews->getForm('new')
			);
			$this->_out('news/newsNew', $viewData);
		}
	}

	function newsMod() {	//modify
		$_modNews = FLEA::getSingleton('Model_News');
		if ( $this->_isPOST() ) {
			$_modNews->update($_POST);
			$this->_throwRemind('修改成功', '新闻已经修改', null, $this->_url('newsAll'));
		} else {
			$news_id = $this->saveGet('news');
			$news = $_modNews->getInfo($news_id);
			$viewData = array(
				'form' => $_modNews->getForm('modify', $news)
			);
			$this->_out('news/newsMod', $viewData);
		}
	}
	
	function newsAll() {
		$_modNews = FLEA::getSingleton('Model_News');
		$ui = FLEA::initWebControls();
		$page = $this->saveGet('page');
		$viewData = $_modNews->getList(null, $page, 15, true);
		$viewData['pagerData'] = $ui->control('pagernav', 'pagenav',
			array('pager'=>$viewData['pagerData'],'controller'=>'admin', 'action'=>'newsAll' ),
			true
		);
		$this->_out('news/newsList', $viewData);
	}

	function newsDel() {
		$_modNews = FLEA::getSingleton('Model_News');
		$news_id = $this->saveGet('news');
		$_modNews->delete($news_id);
		redirect($_SERVER['HTTP_REFERER']);
	}

	/*******************管理员操作**********************************/

	function adminAll() {
		$_modAdmin = FLEA::getSingleton('Model_Admin');
		$viewData = $_modAdmin->getList($this->saveGet('admin'));
		$this->_out('admin', $viewData);
	}

	function adminNew() {
		$_modAdmin = FLEA::getSingleton('Model_Admin');
		if ( $this->_isPOST() ) {
			$result = $_modAdmin->insert($_POST);
			if ( null == $result )
				$this->_throwRemind('添加成功', '管理员已经添加', null, $this->_url('adminAll'));
			else
				$this->_throwRemind('添加失败', null, $result, $this->_url('adminNew'));
		} else {
			$viewData = array(
				'form' => $_modAdmin->getForm(),
				'act' => 'adminNew'
			);
			$this->_out('adminNew', $viewData);
		}
	}

	function adminModify() {
		$_modAdmin = FLEA::getSingleton('Model_Admin');
		if ( $this->_isPOST() ) {
			$_modAdmin->update($_POST);
			$this->_throwRemind('修改成功', '管理员已经修改', null, $this->_url('newsAll'));
		} else {
			$viewData = array(
				'form' => $_modAdmin->getForm($_modAdmin->getInfo($_SESSION[RBAC_USERDATA][userid])),
				'act' => 'adminModify'
			);
			$this->_out('modify', $viewData);
		}
	}

	function adminDele() {
		$_modAdmin = FLEA::getSingleton('Model_Admin');
		$_modAdmin->delete($this->saveGet('admin'));
		redirect($_SERVER['HTTP_REFERER']);
	}

	function adminRole() {
		$_modAdmin = FLEA::getSingleton('Model_Admin');
		if ( $this->_isPOST() ) {

		} else {
			$_modRoles = FLEA::getSingleton('Model_Role');
			$admin = $_modAdmin->getInfo($this->saveGet('admin'));

			foreach ($admin['roles'] as $v)
				$roles[] = $v['role_id'];
			$viewData = array(
				'admin' => $admin,
				'roles' => implode(',', $roles),
				'allroles' => $_modRoles->getAll()
			);
			$this->_out('adminRole', $viewData);
		}
	}
	/*******************管理员操作**********************************/

	/*******************权限管理操作**********************************/

	function rolesManager() {
		$_modRoles = FLEA::getSingleton('Model_Roles');
		$viewData = array(
			'roles' => $_modRoles->getAll()
		);
		$this->_out('rolesManager', $viewData);
	}

	function addRole() {
		$_modRoles = FLEA::getSingleton('Model_Roles');
		if ( $this->_isAjax() ) {
			$role = array(
				'rolename' => $_GET['rolename'],
				'admin_id' => $this->uid,
				'actions' => split(',', $_GET['actions'])
			);
			$role_id = $_modRoles->insert($role);
			echo 'ok_'.$role_id;
		}
	}


	/*******************权限管理员结束**********************************/

	function test() {
		dump($_SESSION);
	}
}
